import EventCenter from "../../TFrame/script/helper/EventCenter";
import EventNames from "../EventNames";
import CoinSystem from "../system/CoinSystem";
import ShopDialog from "../ui/ShopDialog";
import GardenSystem from "./GardenSystem";


const { ccclass, property } = cc._decorator;

@ccclass
export default class UpGradeButton extends cc.Component {

    @property(cc.Label)
    coinLabel: cc.Label = null;

    protected onLoad(): void {
        let flowerIdx = parseInt(this.node.name);
        let coinNeed = GardenSystem.getUpgradeCoinCnt(flowerIdx);
        this.coinLabel.string = coinNeed.toString();
    }

    click(e: cc.Event.EventTouch) {
        let flowerIdx = parseInt(this.node.name);
        let coinNeed = GardenSystem.getUpgradeCoinCnt(flowerIdx);
        if (CoinSystem.cosumeCoin(coinNeed) == true) {
            EventCenter.emit(EventNames.ConsumeCoins)
            GardenSystem.didUpgradePlant(flowerIdx);
            EventCenter.emit(EventNames.PlantUpgraded, flowerIdx);
            this.node.active = false;
        } else {
            //show coin lack
            ShopDialog.show(cc.Canvas.instance.node)
        }
    }

}
